package com.itcam.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcam.model.common.dtos.PageResponseResult;
import com.itcam.model.common.dtos.ResponseResult;
import com.itcam.model.common.enums.AppHttpCodeEnum;
import com.itcam.model.wemedia.dtos.ChannelDto;
import com.itcam.model.wemedia.pojos.WmChannel;
import com.itcam.model.wemedia.pojos.WmNews;
import com.itcam.wemedia.mapper.WmChannelMapper;
import com.itcam.wemedia.service.WmChannelService;
import com.itcam.wemedia.service.WmNewsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author : Cammy.Wu
 * Description : 
 */

@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private WmNewsService wmNewsService;
    @Autowired
    private WmChannelMapper wmChannelMapper;

    /**
     * 查询所有频道
     * @return
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        // 1.检查参数
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2.查询频道
        WmChannel wmChannel = this.getById(id);
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 3.频道是否有效
        if (wmChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道有效，不能删除");
        }
        // 4.判断是否被引用
        int count = wmNewsService.count(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, wmChannel.getId()).eq(WmNews::getStatus, WmNews.Status.PUBLISHED.getCode()));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道被引用，不能删除");
        }
        // 5.删除
        removeById(id);
        // 6.返回
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 频道名称模糊分页查询
     * @param channelDto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(ChannelDto channelDto) {
        // 1.检查参数
        if (channelDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2.检查分页
        channelDto.checkParam();
        // 3.模糊查询+分页
        IPage page = new Page(channelDto.getPage(), channelDto.getSize());
        // 3.频道名称模糊查询
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(channelDto.getName())) {
            wrapper.like(WmChannel::getName, channelDto.getName());
        }
        page = this.page(page, wrapper);
        // 4.返回结果
        PageResponseResult responseResult = new PageResponseResult(channelDto.getPage(), channelDto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 保存频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult insert(WmChannel adChannel) {
        // 1.检查参数
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel wmChannel = this.getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, adChannel.getName()));
        if (wmChannel != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称已存在");
        }
        // 2.保存
        wmChannel.setCreatedTime(new Date());
        this.save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(WmChannel adChannel) {
        // 1.查询参数
        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2.判断是否被引用
        int count = wmNewsService.count(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, adChannel.getId()));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道被引用不能修改或禁用");
        }
        // 3.修改
        updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}




























